package com.sofwin.service.impl;/**
 * @PackageName:com.sofwin.service.impl
 * @author:andyLiu
 * @DateTime:2020/12/24 11:48
 * @Description:[TODO]输入当前模块作用
 **/

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.Constants;
import com.sofwin.dto.UserDto;
import com.sofwin.mapper.SUserMapper;
import com.sofwin.pojo.SUser;
import com.sofwin.pojo.SUserExample;
import com.sofwin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: UserServiceImpl
 * @Description: TODO
 * @Author: andyliu
 * @Date: 2020/12/24 11:48
 * @Version: 1.0
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private SUserMapper userMapper;
    @Override
    public PageInfo<UserDto> queryUserForPage(SUser user, Integer pageNumber) {
        PageHelper.startPage(pageNumber, Constants.PAGE_SIZE);
        List<UserDto> sUsers = userMapper.queryUsers(user);
        return new PageInfo<UserDto>(sUsers);
    }

    @Override
    public boolean deleteUserById(Integer id) {
        int flag = userMapper.deleteByPrimaryKey(id);
        return flag==1?true:false;
    }

    @Override
    public boolean deleteUsers(Integer[] ids) {
        int flag = userMapper.deleteUsers(ids);
        return flag==ids.length?true:false;
    }

    @Override
    public boolean saveUser(SUser user) {
        int flag = 0;
        if(user.getId()==null) {
            flag = userMapper.insertSelective(user);
        }else{
            flag = userMapper.updateByPrimaryKeySelective(user);
        }
        return flag==1?true:false;
    }

    @Override
    public SUser selectUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkLogin(SUser user) {
        boolean status = false;
        SUserExample ex = new SUserExample();
        SUserExample.Criteria criteria = ex.createCriteria();
        criteria.andLoginAccountEqualTo(user.getLoginAccount());
        criteria.andPwdEqualTo(user.getPwd());
        List<SUser> sUsers = userMapper.selectByExample(ex);
        if(sUsers!=null && sUsers.size()==1){
            status = true;
        }
        return status;
    }

    @Override
    public SUser selectUserByLoginAccount(SUser user) {
        SUserExample ex = new SUserExample();
        SUserExample.Criteria criteria = ex.createCriteria();
        criteria.andLoginAccountEqualTo(user.getLoginAccount());
        List<SUser> sUsers = userMapper.selectByExample(ex);
        if(sUsers!=null && sUsers.size()==1){
            return sUsers.get(0);
        }
        return null;
    }
}
